using System.Text;
using System.Windows;
using System.Windows.Markup;

namespace Common
{
	public class DataTemplateCreator
	{
		public static DataTemplate CreateTextBlockDataTemplate(string path)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
			stringBuilder.Append("<Grid>");
			stringBuilder.Append("<TextBlock");
			stringBuilder.Append(" Text=\"{Binding ").Append(path).Append("}\"");
			stringBuilder.Append(" TextAlignment=\"Center\" VerticalAlignment=\"Center\"");
			stringBuilder.Append("/> ");
			stringBuilder.Append("</Grid>");
			stringBuilder.Append("</DataTemplate>");
			return XamlReader.Parse(stringBuilder.ToString()) as DataTemplate;
		}

		public static DataTemplate CreateDatePickerDataTemplate(string path)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">");
			stringBuilder.Append("<Grid FocusManager.FocusedElement=\"{Binding ElementName=editor}\">");
			stringBuilder.Append("<DatePicker Name=\"editor\" BorderThickness=\"0\" Padding=\"0\"");
			stringBuilder.Append(" HorizontalContentAlignment=\"Center\" VerticalContentAlignment=\"Center\"");
			stringBuilder.Append(" Text=\"{Binding ").Append(path).Append(",Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}\"");
			stringBuilder.Append("/>");
			stringBuilder.Append("</Grid>");
			stringBuilder.Append("</DataTemplate>");
			return XamlReader.Parse(stringBuilder.ToString()) as DataTemplate;
		}

		public static DataTemplate CreateTextBoxDataTemplate(string path)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:ctl=\"clr-namespace:Controls;assembly=Controls\">");
			stringBuilder.Append("<Grid FocusManager.FocusedElement=\"{Binding ElementName=editor}\">");
			stringBuilder.Append("<ctl:TextBoxEx Name=\"editor\" BorderThickness=\"0\" Padding=\"0\"");
			stringBuilder.Append(" TextAlignment=\"Center\"");
			stringBuilder.Append(" Text=\"{Binding ").Append(path).Append(", Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}\"");
			stringBuilder.Append("/> ");
			stringBuilder.Append("</Grid>");
			stringBuilder.Append("</DataTemplate>");
			return XamlReader.Parse(stringBuilder.ToString()) as DataTemplate;
		}

		public static DataTemplate CreateComboBoxDataTemplate(string path, string source, string typeName, bool isEditable = false)
		{
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">");
			stringBuilder.Append("<Grid FocusManager.FocusedElement=\"{Binding ElementName=editor}\">");
			stringBuilder.Append("<ComboBox Name=\"editor\" BorderThickness=\"0\" Padding=\"0\"");
			stringBuilder.Append(" Style=\"{DynamicResource ComboBoxDefault}\"");
			stringBuilder.Append(" HorizontalContentAlignment=\"Center\" VerticalContentAlignment=\"Center\"");
			stringBuilder.Append(" ItemsSource=\"{Binding ").Append(source).Append(", RelativeSource={RelativeSource AncestorType={x:Type ")
				.Append(typeName)
				.Append("},Mode=FindAncestor,AncestorLevel=1}}\"");
			stringBuilder.Append(" IsEditable=\"").Append(isEditable ? "True" : "False").Append("\"");
			stringBuilder.Append(" Text=\"{Binding ").Append(path).Append(", Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnSourceUpdated=True}\"");
			stringBuilder.Append(" /> ");
			stringBuilder.Append("</Grid>");
			stringBuilder.Append("</DataTemplate>");
			return XamlReader.Parse(stringBuilder.ToString()) as DataTemplate;
		}
	}
}
